package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    CheckGroupService checkGroupService;
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        //调用service接口的实现类
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //调用service接口的实现类
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);

        return pageResult;
    }

    //根据id查询检查组信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        //调用service接口的实现类
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    //根据id查询关联关系表,checkitem_checkgroup 获取checkitemIds
    @RequestMapping("/findCheckItemIdsByt_checkgroup_checkitem")
    public Result findCheckItemIdsByt_checkgroup_checkitem(Integer id){
        //调用service接口的实现类
        try {
            List<Integer> list = checkGroupService.findCheckItemIdsByt_checkgroup_checkitem(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    //删除检查组信息
    @RequestMapping("/delete")
    public Result delete(Integer id){
        //调用service接口的实现类
        try {
            checkGroupService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            //如果有异常,调用服务失败
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        //调用服务成功
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //编辑检查组信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        //调用service接口的实现类
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/findAll")
    public Result findAll(){

           List<CheckGroup> checkGroupList = checkGroupService.findAll();
           if(checkGroupList!=null&&checkGroupList.size()>0){
               Result result = new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS);
                  result.setData(checkGroupList);
                  return result;

       }

           return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);



    }
}
